// 全局App对象
var A = {};
// 返回对象的dataset值
const C = _obj => {
    return _obj.currentTarget.dataset;
}

// 发起网络请求
const R = (_obj, json) => {
    if (typeof _obj.url != 'undefined') {
        console.log("发起网络请求");
        if(_obj.url.indexOf('http://') == -1 && _obj.url.indexOf('https://') == -1) {
            _obj.url = A.config.host + _obj.url;
        }
        // 获取success方法并处理
        if(typeof _obj.success != 'undefined') {
            _obj["_success"] = _obj.success;
            delete _obj.success;
        }
        // 获取fail方法并处理
        if(typeof _obj.fail != 'undefined') {
            _obj['_fail'] = _obj.fail;
            delete _obj.fail;
        }
        
        let _defObj = {
            url: A.config.host,
            method: 'POST',
            header: {},
            success: res => {
                if (res.statusCode == 200) {
                    _defObj._success(res.data);
                } else {
                    wx.showToast({
                        title: `服务器错误码：${res.statusCode}`,
                        icon: 'none',
                        duration: 1500
                    })
                }
            }, 
            fail: err => {
                wx.showToast({
                    title: `请求超时！当前网络不稳定，请稍后再试...`,
                    icon: 'none',
                    duration: 1500
                })
                _defObj.fail(err);
            }
        }
        // 配置覆盖
        console.log(A.config.req)
        if (typeof A.config.req != 'undefined') {
            // 将默认配置传参扩展至传入对象
            if (typeof A.config.req.data != 'undefined') {
                for (let i in _obj.data) {
                    _obj.data[i] = _obj.data[i] || ''
                }
                _obj.data = Object.assign({}, A.config.req.data, _obj.data);
            }
          let header = json == 1 ? { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' } : {};
            A.config.req.header = header;
            // 请求覆盖
            Object.assign(_defObj, A.config.req);
        }
        // 合并传入的参数对象
        Object.assign(_defObj, _obj);
        // 提交数据
        wx.request(_defObj);
    }
}

// 发起POST网络请求，返回promise对象
const RSP = (_url, josn) => {
    return new Promise((resolve, reject) => {
        let _object = {
            url: _url,
            method: 'POST',
            data: {},
            success: res => {
                resolve(res);
            },
            fail: err => {
                reject(err);
            }
        };
        if (typeof(_url) == 'object') {
            if (_url['data']) _url['data'] = Object.assign(_object.data, _url['data']);
            Object.assign(_object, _url);
        }
        R(_object, josn);
    });
}

// 发起GET网络请求，返回promise对象
const RSG = (_url, josn) => {
    return new Promise((resolve, reject) => {
        let _object = {
            url: _url,
            method: 'GET',
            data: {},
            success: res => {
                resolve(res);
            },
            fail: err => {
                reject(err);
            }
        };
        if (typeof (_url) == 'object') {
            if (_url['data']) _url['data'] = Object.assign(_object.data, _url['data']);
            Object.assign(_object, _url);
        }
        R(_object, josn);
    })
}

// 设置当前模块全局A对象
const setSimpleApp = (_app) => {
    A = _app;
}

module.exports = {
    C,
    R,
    RSP,
    RSG,
    setSimpleApp
}